package 剑指offer;

import java.util.Stack;

/**
 * @description:
 * @author: ywk
 * @date: 2020-10-29
 */
public class 括号序列 {
    public static void main(String[] args) {
        System.out.println(isValid("()"));
    }

    public static boolean isValid(String s) {
        // write code here
        String left = "([{";
        String right = ")]}";
        Stack<Character> stack = new Stack<Character>();

        for (int i = 0; i < s.length(); i++) {
            char index = s.charAt(i);
            if (right.contains(index + "")) {
                if (!stack.isEmpty()) {
                    int r = right.indexOf(index + "");
                    int l = left.indexOf(stack.peek() + "");
                    if (r == l) {
                        stack.pop();
                        continue;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            stack.push(s.charAt(i));

        }
        return stack.isEmpty();
    }
}
